<template>
  <div class="app-container">
    <div class="search-container">
      <el-form inline label-width="68px" ref="queryFromRef" :model="queryParams">
        <el-form-item label="通知类型">
          <el-select placeholder="通知类型" v-model="queryParams.noticeType">
            <el-option label="全部" value="" />
            <el-option label="公告" value="1" />
            <el-option label="会议" value="2" />
            <el-option label="其他" value="99" />
          </el-select>
        </el-form-item>
        <el-form-item label="标题">
          <el-input v-model="queryParams.title" placeholder="标题" />
        </el-form-item>
        <el-form-item label="通知时间">
          <el-date-picker
            :editable="false"
            class="!w-[240px]"
            v-model="queryParams.createTimeArr"
            type="daterange"
            range-separator="~"
            start-placeholder="开始时间"
            end-placeholder="截止时间"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>

        <el-form-item>
          <el-button class="filter-item" type="primary" @click="handleQuery">
            <i-ep-search />
            搜索
          </el-button>
          <el-button @click="handleResetQuery"> <i-ep-refresh />重置 </el-button>

          <el-button
            v-hasPerm="['sys:noticeUser:deleteAll']"
            type="danger"
            @click="handleDeleteAll()"
          >
            <template #icon><i-ep-delete /></template>
            全部删除
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <el-table
        v-loading="loading"
        stripe
        :data="list"
        style="width: 100%"
        @row-click="handleRowClick"
      >
        <el-table-column label="ID" width="50" prop="id" min-width="50" />
        <el-table-column label="标题" width="300" prop="title" />
        <el-table-column label="通知类型" width="100" prop="noticeType">
          <template #default="scope">
            <el-tag v-if="scope.row.noticeType === '1'" size="small" type="success">公告</el-tag>
            <el-tag v-else-if="scope.row.noticeType === '2'" size="small" type="info">会议</el-tag>
            <el-tag v-else size="small" type="warning">其他</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="发送者" width="100" prop="sendUser" />
        <el-table-column label="发送时间" width="200" prop="createTime" />
        <el-table-column label="是否阅读" width="100" prop="readFlag">
          <template #default="scope">
            <el-tag v-if="scope.row.readFlag === true" size="small" type="success">已读</el-tag>
            <el-tag v-else size="small" type="danger">未读</el-tag>
          </template>
        </el-table-column>

        <el-table-column fixed="right" label="操作" width="80">
          <template #default="scope">
            <el-button
              v-hasPerm="['sys:noticeUser:delete']"
              type="danger"
              link
              size="small"
              @click.stop="handleDelete(scope.row.id)"
            >
              <i-ep-delete />
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="handleQuery"
      />
    </el-card>

    <el-drawer v-model="noticeInfoDialog" title="通知详情" @close="handleCloseDialog" size="50%">
      <el-descriptions column="1">
        <el-descriptions-item label="标题">{{ noticeInfo.title }}</el-descriptions-item>
        <el-descriptions-item label="时间">{{ noticeInfo.createTime }}</el-descriptions-item>
        <el-descriptions-item label="类型">{{ noticeInfo.noticeType }}</el-descriptions-item>
        <el-descriptions-item label="内容">{{ noticeInfo.content }}</el-descriptions-item>
      </el-descriptions>

      <template #footer>
        <div class="dialog-footer">
          <el-button @click="handleCloseDialog">取 消</el-button>
        </div>
      </template>
    </el-drawer>
  </div>
</template>

<script setup>
defineOptions({
  name: 'NoticeUser',
  inheritAttrs: false
})

import { ElForm, ElTable } from 'element-plus'
import { ref, reactive, onMounted } from 'vue'
import request from '@/utils/request'

// ======================================================================

const list = ref([])
const total = ref(0)
const loading = ref(false)

const queryFromRef = ref()

const queryParams = reactive({
  title: '',
  noticeType: '',
  pageNum: 1,
  pageSize: 20,
  deleteFlag: false,
  createTimeArr: []
})

function handleResetQuery() {
  console.log('handleResetQuery')
  queryParams.value = {
    title: '',
    noticeType: '',
    pageNum: 1,
    pageSize: 20,
    createTimeArr: []
  }
  handleQuery()
}

function handleQuery() {
  console.log(queryParams, 'queryParams')

  loading.value = true
  request({
    url: '/api/yyplatform/admin/sys/noticeUser/query',
    method: 'get',
    params: {
      pageNum: queryParams.pageNum,
      pageSize: queryParams.pageSize,
      title: queryParams.title,
      noticeType: queryParams.noticeType,
      createTimeArr: queryParams.createTimeArr,
      deleteFlag: queryParams.deleteFlag
    }
  }).then((res) => {
    console.log(res, 'res')

    loading.value = false
    list.value = res.content
    total.value = parseInt(res.totalElements)
  })
}

// ======================================================================

function handleDeleteAll() {
  ElMessageBox.confirm('确认要执行全部删除吗?', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    request({
      url: '/api/yyplatform/admin/sys/noticeUser/deleteAll',
      method: 'post'
    }).then(() => {
      handleQuery()
    })
  })
}
function handleDelete(id) {
  ElMessageBox.confirm('确认删除已选中的数据项?', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    request({
      url: `/api/yyplatform/admin/sys/noticeUser/delete`,
      method: 'post',
      headers: {
        'Content-Type': 'multipart/form-data'
      },
      data: {
        id: id
      }
    }).then(() => {
      handleQuery()
    })
  })
}

// ======================================================================

const noticeInfo = ref({
  title: '',
  noticeType: '',
  content: '',
  createTime: ''
})
const noticeInfoDialog = ref(false)

function handleRowClick(row) {
  request({
    url: '/api/yyplatform/admin/sys/noticeUser/get?id=' + row.id,
    method: 'get'
  }).then((resData) => {
    console.log(resData, '11111res')
    noticeInfoDialog.value = true
    noticeInfo.value = resData
  })
}

function handleCloseDialog() {
  noticeInfoDialog.value = false
  noticeInfo.value = {}
  handleQuery()
}
// ======================================================================

onMounted(() => {
  handleQuery()
})
</script>
